<?php

namespace app\common\dao\system\common;


use app\common\dao\BaseDao;
use app\common\model\BaseModel;
use app\common\model\system\common\Problem;
use think\db\BaseQuery;

/**
 * Class ProblemDao
 * @package app\common\dao\system\common
 * @author fupeng
 * @day 2023/10/7
 */
class ProblemDao extends BaseDao
{

    /**
     * @return string
     * @author fupeng
     * @day 2023/10/7
     */
    protected function getModel(): string
    {
        return Problem::class;
    }

    /**
     * @param array $where
     * @return BaseQuery
     * @author fupeng
     * @day 2023/10/7
     */
    public function search(array $where)
    {
        return Problem::getDB()->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
            $query->where('uid', $where['uid']);
        })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
            $query->whereLike('title', '%'.$where['keyword'].'%');
        })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
            $query->where('common_problem_type_id',$where['type']);
        })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
            $query->where('is_show', $where['status']);
        })->order('create_time DESC');
    }

    /**
     * @param $id
     * @param $uid
     * @return bool
     * @author fupeng
     * @day 2023/10/7
     */
    public function uidExists($id, $uid): bool
    {
        return Problem::getDB()->where($this->getPk(), $id)->where('uid', $uid)->where('is_del', 0)->count($this->getPk()) > 0;
    }

    public function merExists(int $id)
    {
        return $this->getModel()::getDB()->where($this->getPk(), $id)->where('is_del', 0)->count() > 0;
    }
}
